<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="商品编号">
                <a-input v-model="queryParam.numbering" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="商品类别">
                <commodity-category-selection v-model="queryParam.category"></commodity-category-selection>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="是否激活">
                <a-select v-model="queryParam.locking" placeholder="请选择" default-value="">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option value="1">是</a-select-option>
                  <a-select-option value="2">否</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="供应商">
                <supplier-select v-model="queryParam.supplier_id" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button
                  type="primary"
                  @click="$refs.table.refresh(true)"
                >查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
                <a-button style="margin-left: 8px" @click="handleExport">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{ x: 2000 }"
        :data="loadData"
        showPagination="auto"
      >
      </s-table>
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import PartnersForm from '@/views/proxy/partner/Form'
  import { getCultivationAwardList } from '@/api/finance'
  import CommodityCategorySelection from '@/components/CommodityCategorySelection/CommodityCategorySelection'
  import SupplierSelect from '@/components/SupplierSelect/SupplierSelect'
  import { formatAmount } from '@/utils/util'

  const LOCKING_MAP = {
    1: '激活',
    2: '锁定'
  }

  const STATUS_MAP = {
    1: '上架',
    2: '下架'
  }

  export default {
    name: 'List',
    components: {
      SupplierSelect,
      CommodityCategorySelection,
      PartnersForm,
      STable
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '活动编号',
            dataIndex: 'id'
          },
          {
            title: '商品名称',
            dataIndex: 'commodity.name'
          },
          {
            title: '商品编号',
            dataIndex: 'commodity.numbering'
          },
          {
            title: '供应商',
            dataIndex: 'commodity.supplier.name'
          },
          {
            title: '状态',
            customRender: item => STATUS_MAP[item.status]
          },
          {
            title: '是否激活',
            customRender: item => LOCKING_MAP[item.locking]
          },
          {
            title: '总库存',
            customRender: item => item.skus.reduce((total, sku) => sku.stock + total, 0)
          },
          {
            title: '运费',
            customRender: item => formatAmount(item.freight)
          },
          {
            title: '包邮规则',
            width: '150px',
            customRender: item => {
              const freeShippingConditions = []
              let tips = '包邮'
              if (item.free_shipping_maximum && item.free_shipping_maximum > 0) {
                freeShippingConditions.push(`小于${item.free_shipping_maximum}`)
              }
              if (item.free_shipping_minimal && item.free_shipping_minimal > 0) {
                freeShippingConditions.push(`大于等于${item.free_shipping_minimal}`)
              }
              if (freeShippingConditions.length > 0) {
                tips = freeShippingConditions.join(' ') + '件包邮' // 免邮条件
              }
              return tips
            }
          },
          {
            title: '活动时间',
            width: '400px',
            customRender: item => item.panic_buying_start_time + ' 至 ' + item.panic_buying_end_time
          },
          {
            title: '商品类别',
            dataIndex: 'commodity.category.name'
          },
          {
            title: '添加时间',
            width: '200px',
            dataIndex: 'created_at'
          },
          {
            title: '培养奖金额',
            customRender: item => formatAmount(100)
          },
          {
            title: '培养奖数量',
            dataIndex: 'statistics.number_of_cultivation_awards'
          },
          {
            title: '培养奖总金额',
            customRender: item => formatAmount(item.statistics.total_amount_of_cultivation_awards)
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getCultivationAwardList(Object.assign(parameter, this.queryParam))
        }
      }
    },
    created () {

    },
    methods: {
      handleReset () {
        this.queryParam = {}
      },
      handleExport () {
        getCultivationAwardList(Object.assign({ export: true }, this.queryParam))
      }
    }
  }
</script>
